#include "fitsio.h"

int write(char *, float **, long, long);

int write(char *filename, float **m, long nrow, long ncol) {
	
	fitsfile *fptr;
	float z[ncol*nrow];
	int status = 0;
	long fpixel = 1;
	long naxis = 2;
	long naxes[2] = {ncol, nrow};
	long nelements = naxes[0] * naxes[1];
	long i,x,y;
		
	for (x = y = i = 0; i < nrow * ncol; i++, y++) {
		if (y == ncol) {
			y = 0;
			x++;
		}
	
		z[i] = m[x][y];
		
	}
	
	fits_create_file(&fptr, filename, &status);
	fits_create_img(fptr, FLOAT_IMG, naxis, naxes, &status);
	fits_write_img(fptr, TFLOAT, fpixel, nelements, z, &status);
	fits_close_file(fptr, &status);
	fits_report_error(stderr, status);
	
	return status;
}
